package com.example.intermingle.contrllor;

import com.example.intermingle.dto.JqGridPageDto;
import com.example.intermingle.dto.ResourcesEditAndDelDto;
import com.example.intermingle.service.ResourcesService;
import com.example.intermingle.vo.Resources;
import com.example.intermingle.vo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 绿松石
 * @version V1.0
 * @Title ResourcesDto
 * @Description
 * @date 2021年09月15日 10:41
 * @since V1.0
 */
@RestController
@RequestMapping( "/resources" )
public class ResourcesContrllor {

    @Autowired
    ResourcesService resourcesService;

    /**
     * 查询所有的资源，因为删除和更改角色资源，前端页面没法传递角色id，提前把角色id封装到查询所有的方法
     * @param roleId
     * @return
     */
    @RequestMapping( "/selectRoleResources" )
    public JqGridPageDto<ResourcesEditAndDelDto> selectRoleResources(Integer roleId){
        return resourcesService.selectRoleResources( roleId);
    }

    /**
     * 根据id查询单个角色所拥有的资源
     * @param roleId
     * @return
     */
    @RequestMapping( "/selectSingleRoleResources" )
    public JqGridPageDto<Resources> selectSingleRoleResources(Integer roleId){
        return  resourcesService.selectSingleRoleResources(roleId) ;

    }

    /**
     * 给角色添加资源
     * @param roleEditAndDelDtos
     * @return
     */
    @RequestMapping( "/addRoleResources" )
    public JqGridPageDto<Resources> addRoleResources(@RequestBody List<ResourcesEditAndDelDto> roleEditAndDelDtos){
        return resourcesService.addRoleResources(roleEditAndDelDtos);
    }

    /**
     * 给角色删除资源
     * @param roleEditAndDelDtos
     * @return
     */
    @RequestMapping( "/delRoleResources" )
    public JqGridPageDto<Resources> delRoleResources(@RequestBody List<ResourcesEditAndDelDto> roleEditAndDelDtos){
        return resourcesService.delRoleResources( roleEditAndDelDtos);
    }

    /**
     * 查询所有的资源
     * @param page 单页数
     * @param size 但也条数
     * @param resourcesId  资源id
     * @param resourcesName  资源名称
     * @return
     */
    @RequestMapping( "/selectResourcex" )
    public  JqGridPageDto<Resources> selectResourcex(Integer page, Integer size, Integer resourcesId, String resourcesName){

        return  resourcesService.selectResourcex(page,size,resourcesId,resourcesName);

    }

    /**
     * 根据对象添加数据
     * @param resources
     * @return
     */
    @RequestMapping( "/addResources" )
    public JqGridPageDto<Resources> addResources(@RequestBody Resources resources){
        return  resourcesService.addResources(resources);

    }

    /**
     * 根据对象修改数据
     * @param resources
     * @return
     */
    @RequestMapping( "/updateResources" )
    public JqGridPageDto<Resources> updateResources(@RequestBody Resources resources){
        return  resourcesService.updateResources(resources);

    }

    /**
     * 根据资源id删除数据
     * @param resourcesId
     * @return
     */
    @RequestMapping( "/delResources" )
    public JqGridPageDto<Resources> delResources(Integer resourcesId){
        return  resourcesService.delResources(resourcesId);

    }
    /**
     * 导入文件
     * @param request
     * @return
     */
    @RequestMapping("/importResources")
    public JqGridPageDto<Resources> importResources(HttpServletRequest request){
        return resourcesService.imporResources(request);
    }


    /**
     * 导出文件
     * @param pages 页数
     * @param sizes 条数
     * @param response
     */
    @RequestMapping("/exportResources")
    public void exportResources( String pages,  String sizes, HttpServletResponse response){
        Integer page=Integer.parseInt(pages);
        Integer size=Integer.parseInt(sizes);
        resourcesService.exportResources(page,size,response);
    }
}
